package com.edward.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;

import com.edward.JsonDateProcessor;
import com.edward.PagerModel;
import com.edward.model.CommercialList;
import com.edward.model.ContractItem;
import com.edward.model.ContractList;
import com.edward.service.CommercialListManager;
import com.edward.service.ContractListManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CommercialListAction extends ActionSupport{


	private static final long serialVersionUID = 4912812398720905688L;

	private CommercialList commercialList;
	
	private CommercialListManager commercialListManager;
	
	private ContractListManager contractListManager;
	
	private ContractList contractList;
	
	private ContractItem contractItem;
	
	private String query;
	
	private int limit;
	
	private int start;
	
	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}
	
	public ContractList getContractList() {
		return contractList;
	}

	public void setContractList(ContractList contractList) {
		this.contractList = contractList;
	}

	public ContractItem getContractItem() {
		return contractItem;
	}

	public void setContractItem(ContractItem contractItem) {
		this.contractItem = contractItem;
	}



	public void setContractListManager(ContractListManager contractListManager) {
		this.contractListManager = contractListManager;
	}

	public CommercialList getCommercialList() {
		return commercialList;
	}

	public void setCommercialList(CommercialList commercialList) {
		this.commercialList = commercialList;
	}

	public void setCommercialListManager(CommercialListManager commercialListManager) {
		this.commercialListManager = commercialListManager;
	}

	@Secured("ROLE_REPORT_ADD")
	public String add() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		commercialListManager.addCommercialList(commercialList);		
		int total = commercialListManager.listCommercialList().size();
		response.getWriter().print("{success : true , msg : '添加成功!',cmcList_id:"+ commercialList.getId()+",total:" + total + "}");
		return null;
	}
	
	@Secured("ROLE_REPORT_UPDATE")
	public String update() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		commercialListManager.updateCommercialList(commercialList);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_REPORT_UPDATE")
	public String delete() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		CommercialList s = commercialListManager.getCommercialList(commercialList.getId());
		commercialListManager.deleteCommercialList(s.getId());
		response.getWriter().print("{success : true , msg : '删除成功!'}");
		return null;
	}

	@Secured("ROLE_REPORT")
	public String getList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
	    pager = commercialListManager.pagerList(start, limit );
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	
	
	public String getCmcByListNum() throws Exception{
		
		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		List<ContractList> list = contractListManager.listContractList(query);
		Map<String,String> map = new HashMap<String,String>();
		  JSONObject jsonObject=null;
		  String json="";
		for (ContractList c : list) {
			 map.put("num",c.getListNum());
			 jsonObject = JSONObject.fromObject( map );
			 map.clear();
			 json += jsonObject.toString() + ",";
		}
		
		json  = "{\"nums\":[ "  + json.substring(0, json.length()-1) + "]}";
		response.getWriter().print(json);
		return null;
	}

	public String getCmc() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		ContractList  c = contractListManager.getContractList(query);
		Map<String,Object> map = new HashMap<String,Object>();
		JSONObject jsonObject=null;
		map.put("addr", c.getBaddr());
	    map.put("tel", c.getBtel());
		map.put("payment", c.getPayment());
		map.put("mes", c.getBuyer());
		map.put("total", c.getTotal());
		 jsonObject = JSONObject.fromObject( map );
		
		response.getWriter().print(jsonObject.toString());
		return null;
	}
	
	
	@Secured("ROLE_REPORT")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	
	
	
	
	
	

}
